import Tab from '../tab/tab.vue'
import Pages from '../../../pages/pages.vue'
import { getAwardList, updateAward, exportResource, showMsg, setMenuType } from '../../../../vuex/action'

export default {
  name: 'award-list',
  components: {
    'vue-tab': Tab,
    'vue-pages': Pages,
  },
  data() {
    return {
      list: [],
      pages: {
        start: 0,
        totalPages: 0,
        limit: 20
      },
      phone: '',
      status: ''
    }
  },
  vuex: {
    getters: {
      fresh: state => state.dataflowListNeedFresh
    },
    actions: {
      xGetList: getAwardList,
      xUpdateAward: updateAward,
      xShowMsg: showMsg,
      xSetMenuType: setMenuType
    }
  },
  ready() {
    this.xSetMenuType('activity')
    this.getList()
  },
  methods: {
    getList() {
      var param = {
        start: this.pages.start,
        limit: this.pages.limit
      }
      if (this.phone) {
        param.phone = this.phone
      }
      if (this.status) {
        param.status = this.status
      }
      this.xGetList(param)
        .then((data) => {
          this.list = data.list
          this.pages.totalPages = Math.ceil(data.totalCount / this.pages.limit)
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    update(id, type) {
      this.xUpdateAward({
        id: id,
        status: type
      })
        .then((data) => {
          this.xShowMsg({
            type: 'info',
            content: data.error_msg
          })
          this.getList()
        })
        .catch((res) => {
          this.xShowMsg({
            type: 'danger',
            content: res.error_msg
          })
        })
    },
    exportData() {
      var params = '?start=0&limit=1000'
      if (this.phone) {
        params += '&phone=' + this.phone
      }
      if (this.status) {
        params += '&status=' + this.status
      }
      location.href = exportResource +  params
    }
  },
  watch: {
    fresh() {
      if (this.fresh) {
        this.getList({})
      }
    }
  }
}
